﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CardControlLibrary;

namespace CardControlLibrary
{
    public partial class CustomPanel : Panel
    {

        
        public CustomPanel()
        {
            InitializeComponent();
            m_ChildGroups = new List<CardGroupView>();
            m_ChildGroupModel = new List<CardGroupModel>();
            this.AutoScroll = true;
            m_UI_TS = new CardThreadSafeOpperations();
        }

        private CardThreadSafeOpperations m_UI_TS; 
        private List<CardGroupView> m_ChildGroups;
        public List<CardGroupModel> m_ChildGroupModel;

        public void AddGroupView(CardGroupModel cgm)
        {
            m_ChildGroupModel.Add(cgm); 
            CardGroupView cgv = new CardGroupView(cgm, this);
            cgv.OnCollapseExpand += CollapseExpandGroupHandler; 
            m_ChildGroups.Add(cgv);
            m_UI_TS.AddControl(this, cgv);   
        }

        public void ClearView()
        {
            foreach (CardGroupView cgv in m_ChildGroups)
            {
                cgv.ClearGroup();
                m_UI_TS.RemoveControl(this, cgv);
            }
            m_ChildGroups.Clear();
            m_ChildGroupModel.Clear();

        }

        void CollapseExpandGroupHandler(CardGroupView cc)
        {
            DrawControl();
            this.Invalidate();
        }

        public void DrawControl()
        {
            int currentHeight =- this.VerticalScroll.Value; 
            foreach (CardGroupView cgv in m_ChildGroups)
            {
                //this.Controls.Remove(cgv);
                cgv.Location = new Point(0, currentHeight);
                //this.Controls.Add(cgv); 
                cgv.DrawControl();
                currentHeight += cgv.GetGroupHeight; 
            }
            this.VerticalScroll.Visible = true;
        }
    }
}
